// --------------------------------------------------------------------------------------------------------------------
// <copyright file="NavDataFilePlayerAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Test.Robotics.Navigation
{
    using System;
    using System.Diagnostics;
    using System.Runtime.Serialization;
    using Microsoft.Robotics.Interaction.Runtime.Input;
    using Microsoft.Robotics.Navigation.Runtime.GroundTruthExtraction;
    using Microsoft.Robotics.Runtime;
    using Microsoft.Robotics.Vision.Runtime.Cameras;
    using Microsoft.Robotics.Vision.Runtime.GroundTruthExtraction;

    /// <summary>
    /// Agent that calculates the metrics of SLAM.
    /// </summary>
    [DataContract]
    public class NavDataFilePlayerAgent : FilePlayerAgent
    {
        /// <summary>
        /// The name of the agent we use to update the landmark index.
        /// </summary>
        [DataMember(Name = "CameraPoseProducer")]
        private readonly string cameraPoseProducerName = default(string);

        /// <summary>
        /// Camera Pose Estimation Input Forwarder used to pass the UI updates to the camera pose estimation agent.
        /// </summary>
        private IForwarder<CameraPoseEstimationInputMessage> cameraPoseEstimationInputForwarder;

        /// <summary>
        /// Initializes a new instance of the <see cref="NavDataFilePlayerAgent" /> class.
        /// </summary>
        /// <param name="name">Agent name</param>
        /// <param name="navDataFilePath">The file containing the navigation data.</param>
        /// <param name="timerAgent">The timer agent telling us the time</param>
        /// <param name="cameraPoseProducerName">Camera pose producer agent's name</param>
        public NavDataFilePlayerAgent(string name, string navDataFilePath, IProducer<TimerMessage> timerAgent, string cameraPoseProducerName)
            : base(name, navDataFilePath, new[] { typeof(NavDataMessage) }, timerAgent)
        {
            this.cameraPoseProducerName = cameraPoseProducerName;
        }

        /// <summary>
        /// Initialize the agent
        /// </summary>
        /// <param name="locator">Agent locator to use</param>
        public override void Initialize(AgentLocator locator)
        {
            base.Initialize(locator);
            IAgentHost cameraPoseProducerAgent = locator.GetAgent(this.cameraPoseProducerName);
            this.cameraPoseEstimationInputForwarder = cameraPoseProducerAgent.GetControlPort<CameraPoseEstimationInputMessage>();
        }

        /// <summary>
        /// Creates the reader that can parse external dataset files into messages
        /// </summary>
        /// <returns>The new reader</returns>
        protected override IFileReader CreateReader()
        {
            // This file reader will post landmark ID updates to the camera pose estimator.
            return new NavDataFileReader(this.cameraPoseEstimationInputForwarder);
        }
    }
}
